package com.jcgyl.export.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jcgyl.export.dao.TbInventoryBodyDao;
import com.jcgyl.export.dao.TbInventoryHeadDao;
import com.jcgyl.export.dao.TborderheadDao;
import com.jcgyl.export.dao.TborderlistDao;
import com.jcgyl.export.dao.TbpayinfoDao;
import com.jcgyl.export.dao.TbwaybillDao;
import com.jcgyl.export.entity.TbInventoryBody;
import com.jcgyl.export.entity.TbInventoryHead;
import com.jcgyl.export.entity.Tborderhead;
import com.jcgyl.export.entity.Tborderlist;
import com.jcgyl.export.entity.Tbpayinfo;
import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.entity.Tbuploadinfo;
import com.jcgyl.export.entity.Tbwaybill;
import com.jcgyl.export.mapper.TbuploadinfoMapper;
import com.jcgyl.export.model.BaseResponseModel;
import com.jcgyl.export.model.RepushGenerateParameter;
import com.jcgyl.export.mq.MessageSender;
import com.jcgyl.export.util.MessageHelper;
import com.jcgyl.export.util.POIExcelHelper;
import com.jcgyl.export.util.entity.Result;

import jxl.Cell;

@RestController
@RequestMapping("/order/repush")
public class RepushController {
	@Autowired
	private TbuploadinfoMapper tbuploadinfoMapper;
	@Autowired
	private TborderheadDao orderheadDao;
	@Autowired
	private TborderlistDao orderlistDao;
	@Autowired
	private TbpayinfoDao payinfoDao;
	@Autowired
	private TbwaybillDao waybillDao;
	@Autowired
	private TbInventoryHeadDao InventoryHeadDao;
	@Autowired
	private TbInventoryBodyDao InventoryBodyDao;
	@Autowired
	private MessageSender messageSender;

	/**
	 * GenerateParameter list 重推信息
	 */
	@PostMapping("/send")
	@ResponseBody
	public Result Send(@RequestBody RepushGenerateParameter parameter) {
		// Tbsysuser user = (Tbsysuser)
		// SecurityUtils.getSubject().getSession().getAttribute("user");
		// System.out.println("发送的用户" + user.getUname());
		try {
			int uploadId = parameter.getUploadId();
			if(tbuploadinfoMapper.selectByPrimaryKey(uploadId)==null){
				return Result.build(1,"未找到文件路径");
			};
			Tbuploadinfo upload=tbuploadinfoMapper.selectByPrimaryKey(uploadId);
			String file_path = upload.getFilePath();
			List<Cell[]> cell_lists = POIExcelHelper.jxlGetExcelRows(file_path, true);
			System.out.println(cell_lists);
			for (Cell[] cells : cell_lists) {
				String order_no = cells[0].getContents();
				List<String> list = parameter.getList();
				// 查找订单
				if (list.contains("重推订单")) {
					Tborderhead order_head = orderheadDao.findOrderheadByOrderId(order_no);
					List<Tborderlist> order_list = orderlistDao.findOrderlistByOrderId(order_no);
					String order_msg = MessageHelper.GenerateOrderMessage(order_head, order_list);
					messageSender.send(order_msg);
				}
				// 查找运单
				if (list.contains("重推运单")) {
					Tbwaybill waybill = waybillDao.findWaybillByOrderid(order_no);
					String waybill_msg = MessageHelper.GenerateExpressMessage(waybill);
					messageSender.send(waybill_msg);
				}
				// 查找支付单
				if (list.contains("重推支付单")) {
					Tbpayinfo pay = payinfoDao.findPayinfoByOrderid(order_no);
					String pay_msg = MessageHelper.GeneratePayMessage(pay);
					messageSender.send(pay_msg);
				}
				// 查找清单
				if (list.contains("重推清单")) {
					TbInventoryHead inventory_head = InventoryHeadDao.findInventoryHeadByOrderid(order_no);
					List<TbInventoryBody> inventory_bodies = InventoryBodyDao
							.findInventoryBodyByOrderid(inventory_head.getInventoryId());
					String inventory_msg = MessageHelper.GenerateInventoryMessage(inventory_head, inventory_bodies);
					messageSender.send(inventory_msg);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(1, e.getMessage());
		}
		return Result.ok("重推成功");
	}

}
